package com.ruoyi.epidemic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.epidemic.domain.TbDetails;
import com.ruoyi.epidemic.service.ITbDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 省市确诊数据Controller
 * 
 * @author ruoyi
 * @date 2023-03-21
 */
@RestController
@RequestMapping("/epidemic/details")
public class TbDetailsController extends BaseController
{
    @Autowired
    private ITbDetailsService tbDetailsService;

    /**
     * 查询省市确诊数据列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TbDetails tbDetails)
    {
        startPage();
        List<TbDetails> list = tbDetailsService.selectTbDetailsList(tbDetails);
        return getDataTable(list);
    }

    /**
     * 导出省市确诊数据列表
     */
    @PreAuthorize("@ss.hasPermi('epidemic:details:export')")
    @Log(title = "省市确诊数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbDetails tbDetails)
    {
        List<TbDetails> list = tbDetailsService.selectTbDetailsList(tbDetails);
        ExcelUtil<TbDetails> util = new ExcelUtil<TbDetails>(TbDetails.class);
        util.exportExcel(response, list, "省市确诊数据数据");
    }

    /**
     * 获取省市确诊数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('epidemic:details:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbDetailsService.selectTbDetailsById(id));
    }

    /**
     * 新增省市确诊数据
     */
    @PreAuthorize("@ss.hasPermi('epidemic:details:add')")
    @Log(title = "省市确诊数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbDetails tbDetails)
    {
        if (tbDetails.getConfirm()==null) {return AjaxResult.error("确诊人数不行为空");}
        if (tbDetails.getDead()>tbDetails.getConfirm()) {return AjaxResult.error("数据异常，死亡人数大于确诊人数");}
        if (tbDetails.getHeal()>tbDetails.getConfirm()) {return AjaxResult.error("数据异常，治愈人数大于确诊人数");}
        if (tbDetails.getOverseas()>tbDetails.getConfirm()) {return AjaxResult.error("数据异常，境外输入任务大于确诊人数");}
        return toAjax(tbDetailsService.insertTbDetails(tbDetails));
    }

    /**
     * 修改省市确诊数据
     */
    @PreAuthorize("@ss.hasPermi('epidemic:details:edit')")
    @Log(title = "省市确诊数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbDetails tbDetails)
    {
        return toAjax(tbDetailsService.updateTbDetails(tbDetails));
    }

    /**
     * 删除省市确诊数据
     */
    @PreAuthorize("@ss.hasPermi('epidemic:details:remove')")
    @Log(title = "省市确诊数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbDetailsService.deleteTbDetailsByIds(ids));
    }
}
